var xmlHttpRequest=null;
var userid = document.getElementById("userid").value;

//初始化xmlHttpRequest对象
function init()
{         
    try 
    {            
       xmlHttpRequest = new XMLHttpRequest();
    }
    catch (e1)
    {
        try 
        {                
             xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e2) 
        {
            xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
}

//我的首页，显示所有微博
function getAllFeeds()
{
	init();
	var url='http://localhost:8080/ServletStudy/GetFeedServlet?user=all';
	xmlHttpRequest.open('GET',url,true);
	xmlHttpRequest.onreadystatechange=showFeeds;
	xmlHttpRequest.send(null);
}

//我的微博
function getMyFeeds()
{
	init();
	var url='http://localhost:8080/ServletStudy/GetFeedServlet?user=my';
	xmlHttpRequest.open('GET',url,true);
	xmlHttpRequest.onreadystatechange=showFeeds;
	xmlHttpRequest.send(null);
}

//getAllFeeds()和getMyFeeds()的回调函数，显示微博
function showFeeds()
{
	//alert("=初始="+xmlHttpRequest.responseText);
	if(xmlHttpRequest.readyState == 4)
	{
		var str = jsonConvertFeeds(xmlHttpRequest.responseText);
		document.getElementById("feedDiv").innerHTML = str;
	}
}

//发布微博
function publishFeed()
{
	init();
	var content = document.getElementById("publishFeedArea").value;
	alert(content);
	var url="http://localhost:8080/ServletStudy/PublishFeedServlet?content="+content;
	url = encodeURI(url);
	url=  encodeURI(url);
	xmlHttpRequest.open('get',url,true);
	xmlHttpRequest.onreadystatechange=publishFeedResult;
	xmlHttpRequest.send(null);
}

//publishFeed()的回调函数
//发布完成后，刷新显示所有微博
function publishFeedResult()
{
	//alert(xmlHttpRequest.readyState+"=发布完="+xmlHttpRequest.responseText);
	if(xmlHttpRequest.readyState == 4)
	{
		var result = xmlHttpRequest.responseText;
		if(result == "1")
			getAllFeeds();
		else
			alert("Sorry,发布失败！");
	}
}


function jsonConvertFeeds(jsonStr)
{
	var objArr = jsonStr.split(";");
	var outStr = "<table width='80%' align='center'>";
	for(var i=0; i<objArr.length; i++)
	{
		outStr +="<tr>";
		var obj = objArr[i].substring(1, objArr[i].length-1);
		//alert(obj+"==");
		var colArr = obj.split(",");

		outStr += "<td>" + colArr[2].substring(9, colArr[2].length)+ "</td>";
		outStr += "<td>" + colArr[4].substring(8, colArr[4].length) + "</td>";
		outStr += "<td>" + colArr[5].substring(13, colArr[5].length) + "</td>";
		outStr += "</tr>";
		outStr += "<tr>";
		outStr += "<td colspan=3>" + colArr[3].substring(8, colArr[3].length) + "</td>";
		outStr += "</tr>";
		outStr += jsonConvertCommentLink((colArr[0].split(":"))[1]);
		outStr += "<tr>";
		outStr += "<td colspan=3><hr style=\"color:black;border:1px dotted\"></td>";
		outStr += "</tr>";
	}
	outStr += "</table>";
	return outStr;
}

function jsonConvertComments(jsonStr)
{
	var outStr = "";
	if(jsonStr.length > 0)
	{
		var objArr = jsonStr.split(";");
		outStr = "<table width='80%' align='center'>";
		for(var i=0; i<objArr.length; i++)
		{
			outStr +="<tr>";
			var obj = objArr[i].substring(1, objArr[i].length-1);
			//alert(obj+"==");
			var colArr = obj.split(",");
			
			outStr += "<td>" + colArr[0].substring(9, colArr[0].length)+ "</td>";
			outStr += "<td>" + colArr[1].substring(8, colArr[1].length) + "</td>";
			outStr += "<td>" + colArr[3].substring(11, colArr[3].length) + "</td>";
			outStr += "</tr>";
			outStr += "<tr>";
			outStr += "<td colspan=3>" + colArr[2].substring(8, colArr[2].length) + "</td>";
			outStr += "</tr>";
//			outStr += "<tr>";
//			outStr += "<td colspan=3><hr style=\"color:black;border:1px dotted\"></td>";
//			outStr += "</tr>";
		}
		outStr += "</table>";
	}
	else
		outStr = "暂时还没有人评论。";
	return outStr;
}

function jsonConvertCommentLink(fid)
{
	var outStr = "";
	outStr += "<tr>";
	outStr += "<td colspan=3>";
	outStr += "<a href=\"javascript:showAllCommentsLink(" + fid + ")\">查看所有评论</a>  ";
	outStr += "<a href=\"javascript:publishCommentLink(" + fid + ")\">我要评论</a>";
	outStr += "</td></tr>";
	
	outStr += "<tr>";
	outStr += "<td colspan=3>";
	outStr += "<div id=\"publishCommentDiv" + fid + "\" style=display:none>";
	outStr += "<textarea id=\"publishCommentArea" + fid + "\" cols=40 rows=3 value=\"gfdsghf\"></textarea>";
	outStr += "<input type=\"button\" value=\"评论\" onclick=\"javascript:publishComment(" + fid + ");\">";
	outStr += "</div>";
	outStr += "</td></tr>";
	
	outStr += "<tr>";
	outStr += "<td colspan=3>";
	outStr += "<div id=\"showAllCommentsDiv" + fid + "\" style=display:none></div>";
	outStr += "</td></tr>";
	
	return outStr;
}

function showAllCommentsLink(fid)
{
	init();
	//alert("查看评论");
	var url='http://localhost:8080/ServletStudy/GetCommentServlet?fid=' + fid;
	xmlHttpRequest.open('GET',url,true);
	xmlHttpRequest.onreadystatechange=function(){showComments(fid);};
	xmlHttpRequest.send(null);
}

function showComments(fid)
{
	//alert("=初始comment="+xmlHttpRequest.responseText);
	if(xmlHttpRequest.readyState == 4)
	{
		var str = jsonConvertComments(xmlHttpRequest.responseText);
		document.getElementById("showAllCommentsDiv"+fid).innerHTML = str;
		document.getElementById("showAllCommentsDiv"+fid).style.display = "";
	}
}

function publishCommentLink(fid)
{
	document.getElementById("publishCommentDiv" + fid).style.display = "";
}

function publishComment(fid)
{
	var content = document.getElementById("publishCommentArea"+fid).value;
	//alert(content);
	if(content == "")
		alert("请输入评论后再提交！");
	else
	{
		init();
		var url='http://localhost:8080/ServletStudy/PublishCommentServlet?fid=' + fid + "&content="+content;
		url = encodeURI(url);
		url=  encodeURI(url);
		xmlHttpRequest.open('GET',url,true);
		xmlHttpRequest.onreadystatechange=publishCommentResult;
		xmlHttpRequest.send(null);
	}
}

function publishCommentResult()
{
	//alert(xmlHttpRequest.readyState+"=发布完comment="+xmlHttpRequest.responseText);
	if(xmlHttpRequest.readyState == 4)
	{
		var result = xmlHttpRequest.responseText;
		if(result == "1")
			getAllFeeds();
		else
			alert("Sorry,评论失败！");
	}
}